import pygame

from daxiong.util import Util


class UISprite(pygame.sprite.Sprite):
    def __init__(self, name, center):
        super().__init__()
        self.image = pygame.image.load(f"./image/{name}")
        self.rect = self.image.get_rect()
        self.rect.center = center





class UIManage:
    def __init__(self, gm):
        self.score = 0
        self.font = pygame.font.Font("./font/Inkfree.ttf", 24)
        self.score_label = self.font.render(f"Score: {self.score}", True, "red")
        self.gm = gm

        self.ready_group = pygame.sprite.Group()
        self.begin_btn = UISprite("begin_btn.png", (400, 300))
        self.begin_btn.add(self.ready_group)


        self.end_group = pygame.sprite.Group()
        self.restart_btn = UISprite("replay_btn.png", (400, 300))
        self.restart_btn.add(self.end_group)

    def add_score(self):
        self.score += 1
        self.score_label = self.font.render(f"Score: {self.score}", True, "red")

    def update(self):
        if self.gm.state == "ready":
            self.ready_group.draw(self.gm.screen)
            self.ready_group.update()

            if Util.check_click(self.begin_btn) and self.gm.state == "ready":
                self.gm.state = "gaming"

        if self.gm.state == "gaming":
            self.gm.screen.blit(self.score_label, (700, 20))

        if self.gm.state == "end":
            self.end_group.draw(self.gm.screen)
            self.end_group.update()

            if Util.check_click(self.restart_btn) and self.gm.state == "end":
                self.gm.state = "gaming"
                self.score = 0
                self.score_label = self.font.render(f"Score: {self.score}", True, "red")

